package com.vgemv.jsutilitysdk.activity.jsloglistactivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdk.R;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

public class JsLogListActivity extends AppCompatActivity {

    public static JsLogFileItemVm mLogFileItemVm;

    RelativeLayout mRl_root = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRl_root = new RelativeLayout(this);
        setContentView(mRl_root);

        //创建数据源.
        String logHome = JsLogManager.sharedInstance().getLogDirPath();
        File file_logHome = new File(logHome);
        FileFilter fileFilter = new FileFilter() {
            @Override
            public boolean accept(File file) {
                if(file.isFile()){
                    return true;
                }
                return false;
            }
        };
        File logFileList[]= file_logHome.listFiles(fileFilter);

        ArrayList<JsMenuItemVm> al = new ArrayList<>();
        for(File file:logFileList) {
            JsLogFileItemModel fileModel = new JsLogFileItemModel(file);
            JsLogFileItemVm vm = new JsLogFileItemVm(fileModel);
            al.add(vm);
        }

        JsMenuView menuView = new JsMenuView(this);
        menuView.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                return al;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView menuView, int i, JsMenuItemVm vm) {
                JsLogManager.logfc_d("JsLogListActivity","click at index:%d",i);
                mLogFileItemVm = (JsLogFileItemVm) vm;
                Intent intent = new Intent(JsLogListActivity.this,JsLogDetailActivity.class);
                startActivity(intent);
            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView menuView, Context context) {
                JsLogFileItemView itemView = new JsLogFileItemView(context);
                return itemView;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView menuView) {

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView menuView) {
                JsMenuViewVm vm = new JsMenuViewVm();
                vm.mi_layoutStyle = JsMenuViewVm.JsMenuViewLayoutStyleVerticalList;
                vm.mColor_background = Color.rgb(200,200,200);
                return vm;
            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView menuView, int index) {
                int margin = 20;
                Rect rect = new Rect();
                if(index == 0) {
                    rect.top = margin;
                }
                else {
                    rect.top = 0;
                }
                rect.left = rect.right = margin;
                rect.bottom = margin;
                return rect;
            }

            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }
        };

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mRl_root.addView(menuView,lp);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem_clear = menu.add("清空");
        menuItem_clear.setEnabled(true);
        menuItem_clear.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menuItem_clear.setIcon(R.mipmap.trash);
        menuItem_clear.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(@NonNull MenuItem menuItem) {
                JsLogManager.logfc_d("JsLogListActivity","click delete item.");
                return true;
            }
        });
        //return false,不会显示.return true才会显示.
        return true;
    }





}
